from django.contrib import admin

from .models import Post, Tag, Category


class PostAdmin(admin.ModelAdmin):
    list_display = ('id', 'title', 'desc', 'content', 'status', 'category', 'owner', 'created_time')
    list_filter = ('tag', 'status', 'created_time')
    search_fields = ('title', 'content', 'tag')

    fieldsets = [
        (None, {'fields': ['title']}),
        ('基本信息', {'fields': ['content', 'desc', 'status', 'category', 'tag', 'owner']}),
    ]

    def save_model(self, request, obj, form, change):
        if not change:
            obj.owner = request.user
        obj.save()


class TagAdmin(admin.ModelAdmin):
    list_display = ('id', 'name', 'status', 'owner', 'created_time')

    fieldsets = [
        (None, {'fields': ['name']}),
    ]

    def save_model(self, request, obj, form, change):
        if not change:
            obj.owner = request.user
        obj.save()


class CategoryAdmin(admin.ModelAdmin):
    list_display = ('id', 'name', 'status', 'is_nav', 'owner', 'created_time')

    fieldsets = [
        (None, {'fields': ['name', 'is_nav']}),
    ]

    def save_model(self, request, obj, form, change):
        if not change:
            obj.owner = request.user
        obj.save()


admin.site.register(Post, PostAdmin)
admin.site.register(Tag, TagAdmin)
admin.site.register(Category, CategoryAdmin)

admin.site.site_title = "DjangoBLog后台管理"
admin.site.site_header = "DjangoBLog后台管理"
